﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Drawing;
using Emgu.CV.Structure;
using AsyncCamera.Camera.Structure;
using Incendios0_1.Camera.wpfdatamanager;
using System.Configuration;
using AsyncCamera.Camera.media;

namespace AsyncCamera.Camera.Pelco
{
    class FrameGrabber
    {
        public MediaError? error;

        private Frame frame;
        public Frame Frame
        {
            get { return this.frame; }
            set { this.frame = value; }
        }

        private AbstractCamera attachedCamera;
        public AbstractCamera AttachedCamera
        {
            get { return this.attachedCamera; }
            set { this.attachedCamera = value; }
        }

        private IPCameraSource feed;

        public FrameGrabber(IPCameraSource feed, double id)
        {
            this.attachedCamera = feed.AttachedCamera;
            frame = new Frame();
            frame.Id = id;
            this.feed = feed;
            frame.Error = MediaError.None;
            error = null;
            QueryFrame();
        }

        private void QueryFrame()
        {
            try
            {
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(attachedCamera.cameraData.Url);
                try
                {
                    myRequest.Timeout = Int32.Parse(ConfigurationManager.AppSettings["WaitForCamera"]);
                }
                catch
                {
                    myRequest.Timeout = 3000;
                }
                myRequest.Method = "GET";
                myRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), myRequest);
            }
            catch (Exception ex)
            {
                frame.Error = MediaError.IPHostUnreachable;
                frame.Message = ex.Message;
                feed.Frame = frame;
                error = MediaError.IPHostUnreachable;
                feed.Grabbers.Remove(frame.Id);
                feed.Errors += 1;
            }
        }

        void FinishWebRequest(IAsyncResult result)
        {
            try
            {
                HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
                Bitmap bmp = new Bitmap(response.GetResponseStream());
                // close off the stream and the response
                response.Close();
                // return the Bitmap of the image
                frame.Error = MediaError.OK;
                Emgu.CV.Image<Bgr, Byte> img = new Emgu.CV.Image<Bgr, Byte>(bmp);
                frame.Image = img;
                feed.Frame = frame;
                feed.Grabbers.Remove(frame.Id);
            }
            catch (Exception ex)
            {
                frame.Error = MediaError.IPHostUnreachable;
                frame.Message = ex.Message;
                feed.Frame = frame;
                error = MediaError.IPHostUnreachable;
                feed.Grabbers.Remove(frame.Id);
                feed.Errors += 1;
            }
        }
    }
}
